@extends('admin.common.base-master')

@section('html-head')
    <link href="{{url('css/admin/pagenum.css')}}" rel="stylesheet" />

@endsection

@section('html-body')
    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <a href="#">行业管理</a>
            <em></em>
            <a href="{{route('Admin.Home.tradeList')}}">行业列表</a>
        </div>

        <!-- 二级目录 -->
        @include('admin.common.trade-header')

        <div class="filter-box clearfix">
            <div class="fr">
                <a class="addBtn" href="javascript:;" onclick="upPop(0)">新增</a>
            </div>
        </div>

        <div class="table-box">
            <table class="table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>行业名称</th>
                    <th>排序</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @if(count($trades) > 0)
                    @foreach($trades as $key=>$trade)
                        <tr id="classify_{{$trade->id}}">
                            <td>{{$trade->id}}</td>
                            <td>
                                <span id="classify_name_{{$trade->id}}">{{$trade->name}}</span>
                            </td>
                            <td>{{$trade->sort}}</td>
                            <td>
                                <a href="javascript:void(0);" data-id="{{$trade->id}}" style="color: #ef1414;" onclick="upPop({{$trade}})">编辑</a> |
                                <a href="javascript:void(0);" class="del" data-id="{{$trade->id}}" >删除</a>
                            </td>
                        </tr>
                    @endforeach
                @endif
                </tbody>
            </table>
        </div>
        <div class="pagelist" style="text-align: center;">
            <?php echo $trades->render(); ?>
        </div>
    </div>

    <div class="upPop-box">
        <div class="backdrop"></div>
        <div id="upPop" class="info-pop tixian">
            <input type="hidden" name="id" id="tradeid" value=""/>
            <a class="close" href="javascript:void(0);"></a>
            <h3>行业设置</h3>
            <div class="fill-list">
                <span class="fill-tit">行业名称：</span>
                <input class="fill-ipt" type="text" name="name" id="name" value="">
            </div>


            <div class="fill-list">
                <span class="fill-tit">排序:</span>
                <input class="fill-ipt" type="text" name="sort" id="sort" value="">
            </div>

            <input type="hidden" name="id" id="trade_id" />
            <div class="fill-list">
                <span class="fill-tit">&nbsp;</span>
                <input class="fill-submit" type="button" id="subThisForm" value="提交">
            </div>
        </div>
    </div>

    <script>
        function upPop(obj){
            $('#tradeid').val(obj.id);
            $('#name').val(obj.name);
            $('#sort').val(obj.sort);
            $('.upPop-box').upPop();

        }

        $(function(){
            $(document).on('click', '#subThisForm', function () {
                var name = $('#name').val();
                var sort  = $('#sort').val();
                var id = $('#tradeid').val();

                $.ajax({
                    url: '{{route("Api.Admin.sys.addTrade")}}',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        name:name,
                        sort:sort,
                        id:id
                    },
                    success: function (obj) {
                        if (obj.level == "success") {
                            noty({
                                text: obj.message, type: obj.level, timeout: 600,
                                callback: {
                                    afterClose:function(){
                                        window.location.reload();
                                    }
                                }
                            });
                        } else if (obj.level == "error") {
                            noty({text: obj.message, type: obj.level, timeout: 600});
                        }

                    },
                    error: function (obj) {
                        console.log(obj);
                    }
                });
            });

            $(document).on("click",".del",function(){
                var id = $(this).attr("data-id");
                noty({
                    text: '可能影响职位内容，你确定要删除吗?',
                    buttons: [
                        {addClass: 'btn btn-primary', text: '确定', onClick: function($noty) {
                                $noty.close();
                                $.ajax({
                                    url:'{{route('Api.Admin.sys.delTrade')}}',
                                    type:'post',
                                    dataType:'json',
                                    data:{id:id},
                                    success:function(obj){
                                        if(obj.level == "success"){
                                            $("#classify_"+id).hide('slow');
                                        }else{
                                            noty({type:obj.level,text:obj.message,timeout:600});
                                        }
                                    },
                                    error:function(obj){
                                        console.log(obj);
                                    }

                                });
                            }
                        },
                        {addClass: 'btn btn-danger', text: '取消', onClick: function($noty) {
                                $noty.close();
                            }
                        }
                    ]
                });

            });
        });
    </script>
@endsection